package com.rocksea.app.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rocksea.app.tasks.AskTask;
import com.rocksea.app.R;
import com.rocksea.app.adapter.PileListAdapter;
import com.rocksea.app.entry.InspectionForm;
import com.rocksea.app.entry.PileInfo;
import com.rocksea.app.utils.UiUtils;
import com.rocksea.app.weight.DividerListItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * Created by 吴志华 on 2017/8/10.
 *
 */

public class PilNoListFragment extends Fragment {

    private RecyclerView mRecyclerView;


    public static PilNoListFragment newInstance(Bundle args)
    {
        PilNoListFragment fragment = new PilNoListFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);  //注册菜单
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        EventBus.getDefault().register(this);
        View mBootView = inflater.inflate(R.layout.pile_list_layout,container,false);
        mRecyclerView = (RecyclerView) mBootView.findViewById(R.id.Pile_List);
        //new InspcItemAdpter(getContext(), mInfos,R.layout.inspe_list_item)
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        mRecyclerView.setLayoutManager(layoutManager); //不添加这句 不显示
        mRecyclerView.addItemDecoration(new DividerListItemDecoration(getContext(),DividerListItemDecoration.VERTICAL_LIST));
        queryDataBySerilNo();
        return mBootView;
    }
    //根据流水号查询数据库
    private void queryDataBySerilNo()
    {
        InspectionForm.InspectionFormListBean mFormListBean = (InspectionForm.InspectionFormListBean) getArguments().get(UiUtils.BUNDLE_KEY);
        if (mFormListBean ==null)
            return;
        String serilNo =  mFormListBean.number;
        int methodCode = UiUtils.judgMentMethod(mFormListBean.applyMethod);
        AskTask task = new AskTask(methodCode,serilNo, (ArrayList<InspectionForm.PileBean>) mFormListBean.pileList);
        task.execute();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onQueryCompete(ArrayList<PileInfo> tables)
    {
        PileListAdapter listAdapter = new PileListAdapter(tables, getContext(), PileListAdapter.Q);
        mRecyclerView.setAdapter(listAdapter);
    }

    @Override
    public void onDestroy()
    {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

}
